package com.fjsh.base.exception;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fjsh.enums.HttpStatus;
import java.io.IOException;

/**
 * 自定义异常返回
 */
public class CustomOauthExceptionSerializer extends StdSerializer<CustomOauthException> {
    private static final long serialVersionUID = 1L;

    public static final String BAD_CREDENTIALS = "Bad credentials";

    public CustomOauthExceptionSerializer() {
        super(CustomOauthException.class);
    }

    @Override
    public void serialize(CustomOauthException e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeNumberField("message", HttpStatus.EXCEPTION.getCode());
        if (StrUtil.equals(e.getMessage(), BAD_CREDENTIALS)) {
            jsonGenerator.writeStringField("message", "用户名或密码不正确");
        } else {
            jsonGenerator.writeStringField("message", e.getMessage());
        }
        jsonGenerator.writeEndObject();
    }
}